const { createUser, updateUser, deleteUser, getUser, getUsers, getStudyRanking } = require('../controllers/users');
const logger = require('../config/logger');

async function userRoutes(fastify, options) {

  // 创建新用户
  fastify.post('/users', {
    schema: {
      description: '创建新用户',
      tags: ['用户管理'],
      body: {
        type: 'object',
        required: ['name', 'email', 'password'],
        properties: {
          name: { type: 'string', description: '用户名称' },
          avatar: { type: 'string', description: '用户头像URL' },
          email: { type: 'string', description: '用户邮箱' },
          phone: { type: 'string', description: '手机号码' },
          facebookId: { type: 'string', description: 'Facebook账号ID' },
          role: { type: 'string', description: '用户角色' },
          password: { type: 'string', description: '用户密码' }
        }
      },
      response: {
        201: {
          type: 'object',
          description: '创建成功',
          properties: {
            id: { type: 'number', description: '用户ID' },
            name: { type: 'string', description: '用户名称' },
            avatar: { type: 'string', description: '用户头像URL' },
            email: { type: 'string', description: '用户邮箱' },
            phone: { type: 'string', description: '手机号码' },
            facebookId: { type: 'string', description: 'Facebook账号ID' },
            role: { type: 'string', description: '用户角色' },
            balance: { type: 'number', description: '账户余额' },
            totalStudyTime: { type: 'number', description: '总学习时间（分钟）' },
            totalReservations: { type: 'number', description: '总预约次数' },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
          }
        },
        400: {
          type: 'object',
          description: '请求参数错误',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    handler: createUser
  });

  // 获取用户列表
  fastify.get('/users', {
    schema: {
      description: '获取所有用户列表',
      tags: ['用户管理'],
      response: {
        200: {
          type: 'array',
          description: '用户列表',
          items: {
            type: 'object',
            properties: {
              id: { type: 'number', description: '用户ID' },
              name: { type: 'string', description: '用户名称' },
              avatar: { type: 'string', description: '用户头像URL' },
              email: { type: 'string', description: '用户邮箱' },
              phone: { type: 'string', description: '手机号码' },
              facebookId: { type: 'string', description: 'Facebook账号ID' },
              role: { type: 'string', description: '用户角色' },
              balance: { type: 'number', description: '账户余额' },
              totalStudyTime: { type: 'number', description: '总学习时间（分钟）' },
              totalReservations: { type: 'number', description: '总预约次数' },
              createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
              updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
            }
          }
        }
      }
    },
    preHandler: [fastify.authenticate],
    handler: getUsers
  });

  // 获取单个用户
  fastify.get('/users/:id', {
    schema: {
      description: '获取指定用户信息',
      tags: ['用户管理'],
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', description: '用户ID' }
        }
      },
      response: {
        200: {
          type: 'object',
          description: '用户信息',
          properties: {
            id: { type: 'number', description: '用户ID' },
            name: { type: 'string', description: '用户名称' },
            avatar: { type: 'string', description: '用户头像URL' },
            email: { type: 'string', description: '用户邮箱' },
            phone: { type: 'string', description: '手机号码' },
            facebookId: { type: 'string', description: 'Facebook账号ID' },
            role: { type: 'string', description: '用户角色' },
            balance: { type: 'number', description: '账户余额' },
            totalStudyTime: { type: 'number', description: '总学习时间（分钟）' },
            totalReservations: { type: 'number', description: '总预约次数' },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' },
            memberships: {
              type: 'array',
              description: '用户的会员资格',
              items: {
                type: 'object',
                properties: {
                  id: { type: 'number', description: '会员ID' },
                  membershipType: { type: 'string', description: '会员类型' },
                  balance: { type: 'number', description: '会员余额' },
                  expiresAt: { type: 'string', format: 'date-time', description: '过期时间' }
                }
              }
            },
            orders: {
              type: 'array',
              description: '用户的订单',
              items: {
                type: 'object',
                properties: {
                  id: { type: 'number', description: '订单ID' },
                  status: { type: 'string', description: '订单状态' },
                  orderStartTime: { type: 'string', format: 'date-time', description: '预约开始时间' },
                  orderEndTime: { type: 'string', format: 'date-time', description: '预约结束时间' }
                }
              }
            }
          }
        },
        404: {
          type: 'object',
          description: '用户不存在',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    preHandler: [fastify.authenticate],
    handler: getUser
  });

  // 更新用户信息
  fastify.put('/users/:id', {
    schema: {
      description: '更新用户信息',
      tags: ['用户管理'],
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', description: '用户ID' }
        }
      },
      body: {
        type: 'object',
        properties: {
          name: { type: 'string', description: '用户名称' },
          avatar: { type: 'string', description: '用户头像URL' },
          email: { type: 'string', description: '用户邮箱' },
          phone: { type: 'string', description: '手机号码' },
          facebookId: { type: 'string', description: 'Facebook账号ID' },
          role: { type: 'string', description: '用户角色' },
          balance: { type: 'number', description: '账户余额' },
          totalStudyTime: { type: 'number', description: '总学习时间（分钟）' },
          totalReservations: { type: 'number', description: '总预约次数' },
          password: { type: 'string', description: '用户密码' }
        }
      },
      response: {
        200: {
          type: 'object',
          description: '更新成功',
          properties: {
            id: { type: 'number', description: '用户ID' },
            name: { type: 'string', description: '用户名称' },
            avatar: { type: 'string', description: '用户头像URL' },
            email: { type: 'string', description: '用户邮箱' },
            phone: { type: 'string', description: '手机号码' },
            facebookId: { type: 'string', description: 'Facebook账号ID' },
            role: { type: 'string', description: '用户角色' },
            balance: { type: 'number', description: '账户余额' },
            totalStudyTime: { type: 'number', description: '总学习时间（分钟）' },
            totalReservations: { type: 'number', description: '总预约次数' },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
          }
        },
        404: {
          type: 'object',
          description: '用户不存在',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    preHandler: [fastify.authenticate],
    handler: updateUser
  });

  // 删除用户
  fastify.delete('/users/:id', {
    schema: {
      description: '删除指定用户',
      tags: ['用户管理'],
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', description: '用户ID' }
        }
      },
      response: {
        204: {
          type: 'null',
          description: '删除成功'
        },
        404: {
          type: 'object',
          description: '用户不存在',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    preHandler: [fastify.authenticate],
    handler: deleteUser
  });

  // 获取学习排行榜
  fastify.get('/users/ranking/study', {
    schema: {
      description: '获取学习排行榜（按总学习时间排序）',
      tags: ['用户管理'],
      response: {
        200: {
          type: 'array',
          description: '学习排行榜',
          items: {
            type: 'object',
            properties: {
              id: { type: 'number', description: '用户ID' },
              name: { type: 'string', description: '用户名称' },
              avatar: { type: 'string', description: '用户头像URL' },
              totalStudyTime: { type: 'number', description: '总学习时间（分钟）' },
              totalReservations: { type: 'number', description: '总预约次数' }
            }
          }
        }
      }
    },
    handler: getStudyRanking
  });

}

module.exports = userRoutes;